<?php
class specialModel extends baseModel{
    protected $table ='cms_special';  //模型表
    //添加
    public function add($data = array()){
        $info = $this->insert($data); //插入模型数据
        if($info){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //删除
    public function del($fid){
        $subclass = $this->find('pid = '.$fid);
        if($subclass){
            return FALSE;
        }
        $result = $this->delete('id = '.$fid);
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //编辑
    public function edit($fid,$data = array()){
        $condition['id'] = $fid;
        $info = $this->update($condition,$data);
        if($info){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //当前路径
    public function flpath($pid = 0,$url = null) {
        $condition['id'] = intval($pid);
        $result = $this->find($condition,'id,pid,name');
        if ($result) {
            $Str = $this->flpath($result['pid'],$url);
            if($url){
                $Str .= '<a href="' . url(''.$url.'',array(id=>$result['id'])).'">' . $result['name'] . '</a>';
            }else{
                $Str .= '<a href="###">' . $result['name'] . '</a>';
            }
        }
        return $Str;
    }
    //获取分类树，$id，分类id,$id=0，获取所有分类结构树
    public function getClass($id=0) {
        $data = $this->select('','id,pid,name'); //第一种 
        $cat = new Category(array('id', 'pid', 'name', 'cname'));
        return $cat->getTree($data, $id);
    }
    //资源特性下拉菜单
    //$rootid:特性跟目录ID,$cid:特性所在栏目,$select_id:文字所选特性
    public function CreateSpecialTree($cid,$select_id = null,$topli = FALSE,$pid = 0){
       if($select_id){
           $str = explode(',',$select_id);
       }
       $pid = intval($pid);
       $rel = $this->select('pid = '.$pid.' and (cid=0 or cid='.$cid.')','id,name');
       if($rel){
            if($topli)
                echo '<ul>';
            foreach($rel as $key => $rs){
                echo '<li> <input ';
                if(is_array($str)){
                    if(in_array($rs['id'], $str)){
                        echo 'checked="checked" ';
                    }
                }
               echo 'type="checkbox" name="special[]" value="'.$rs['id'].'"> '.$rs['name'];
               $this->CreateSpecialTree($cid,$select_id,TRUE,$rs['id']);
               echo '</li>';
            }
            if($topli)
                echo '</ul>';
       }
    }
}